﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using VASJ.Service.Library;
using VASJ.Model;
using System.Web.Profile;
using VASJ.Service.Library.Manager;
using System.Security.Permissions;

namespace VASJ.Service
{
    public partial class AcpInetService
    {
        #region "AHSHelper"
        //[PrincipalPermission(SecurityAction.Demand, Role = "ahs_service")]
        /// <summary>
        /// IsUserInProject
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="username"></param>
        /// <param name="projectid"></param>
        /// <returns></returns>
        public bool IsUserInProject(string ApplicationName, string username, int projectid)
        {
            bool ret = false;

            try
            {
                UserManager um = new UserManager(ApplicationName);

                ret = um.IsUserInProject(username, projectid);
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Helper.IsUserInProject error {0}", ex);
            }

            return ret;
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "ahs_service")]
        /// <summary>
        /// GetProject
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="projectId"></param>
        /// <returns></returns>
        public Project GetProject(string ApplicationName, int projectId)
        {
            Project ret = null;

            try
            {
                ProjectManager pm = new ProjectManager(ApplicationName);

                ret = pm.GetProject(projectId);
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Helper.GetProject error {0}", ex);
            }

            return ret;
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "ahs_service")]
        /// <summary>
        /// GetProjectForUser
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="username"></param>
        /// <param name="include_via_org"></param>
        /// <returns></returns>
        public Dictionary<string, Project> GetProjectForUser(string ApplicationName, string username, bool include_via_org = false)
        {
            Dictionary<string, Project> ret = null;

            try
            {
                ProjectManager pm = new ProjectManager(ApplicationName);

                ret = pm.GetProjectForUser(username, include_via_org);
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Helper.GetProjectForUser error {0}", ex);
            }

            return ret;
        }

        /// <summary>
        /// Get Project For User, Role, projectType, ownerId
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="username"></param>
        /// <param name="rolename"></param>
        /// <param name="projectType"></param>
        /// <param name="ownerId"></param>
        /// <param name="include_via_org"></param>
        /// <returns></returns>
        public List<Project> GetProjectsForUser(string ApplicationName, string username, string rolename, string projectType, Guid ownerId, bool include_via_org = false)
        {
            List<Project> ret = null;
            try
            {
                ProjectManager pm = new ProjectManager(ApplicationName);

                ret = pm.GetProjectForUser(username, rolename, projectType, ownerId, include_via_org);
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Helper.GetProjectsForUser error {0}", ex);
            }

            return ret;
        }

        /// <summary>
        /// Get project For user, projectType
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="username"></param>
        /// <param name="projectType"></param>
        /// <param name="include_via_org"></param>
        /// <returns></returns>
        public List<Project> GetProjectsForUserNotRole(string ApplicationName, string username,  string projectType, bool include_via_org = false)
        {
            List<Project> ret = null;
            try
            {
                ProjectManager pm = new ProjectManager(ApplicationName);

                ret = pm.GetProjectForUser(username, projectType, include_via_org);
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Helper.GetProjectsForUserNotRole error {0}", ex);
            }

            return ret;
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "ahs_service")]
        /// <summary>
        /// GetProjectInvitedForUser
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        public Dictionary<string, Project> GetProjectInvitedForUser(string ApplicationName, string userId)
        {
            Dictionary<string, Project> ret = null;

            try
            {
                ProjectManager pm = new ProjectManager(ApplicationName);

                ret = pm.GetProjectInvitedForUser(userId);
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Helper.GetProjectInvitedForUser error {0}", ex);
            }

            return ret;
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "ahs_service")]
        /// <summary>
        /// GetProjectForUserInRole
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="username"></param>
        /// <param name="rolename"></param>
        /// <param name="projectType"></param>
        /// <param name="projectType"></param>
        /// <param name="linkProject"></param>
        /// <param name="include_via_org"></param>
        /// <returns></returns>
        public int[] GetProjectForUserInRole(string ApplicationName, string username, string rolename, string projectType, int linkProject,bool include_via_org = false)
        {
            int[] ret = null;

            try
            {
                ProjectManager pm = new ProjectManager(ApplicationName);

                ret = pm.GetProjectForUser(username, rolename,projectType,linkProject, include_via_org);
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Helper.GetProjectForUserInRole error {0}", ex);
            }

            return ret;
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "ahs_service")]
        /// <summary>
        /// GetProjectForUserNotByRole
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="username"></param>
        /// <param name="include_via_org"></param>
        /// <returns></returns>
        public Dictionary<string, Project> GetProjectForUserNotByRole(string ApplicationName, string username, bool include_via_org = false)
        {
            Dictionary<string, Project> ret = null;

            try
            {
                ProjectManager pm = new ProjectManager(ApplicationName);

                ret = pm.GetProjectForUserNotByRole(username, include_via_org);
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Helper.GetProjectForUserNotByRole error {0}", ex);
            }

            return ret;
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "ahs_service")]
        /// <summary>
        /// GetUserInProject
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="projectid"></param>
        /// <returns></returns>
        public Dictionary<Guid, string> GetUserInProject(string ApplicationName, int projectid)
        {
            Dictionary<Guid, string> ret = null;

            try
            {
                UserManager um = new UserManager(ApplicationName);

                ret = um.GetUserInProject(projectid);
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Helper.GetUserInProject error {0}", ex);
            }

            return ret;
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "ahs_service")]
        /// <summary>
        /// Author: Le Anh Phong
        /// Get the user list is exist in Project not via Organization
        /// Project Request: CS
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="projectid"></param>
        /// <returns></returns>
        public Dictionary<Guid, User> GetListUserInProject(string ApplicationName, int projectid)
        {
            Dictionary<Guid, User> ret = null;

            try
            {
                UserManager um = new UserManager(ApplicationName);

                ret = um.GetListUserInProject(projectid);
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Helper.GetListUserInProject error {0}", ex);
            }

            return ret;
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "ahs_service")]
        /// <summary>
        /// Author: Hoang Van Tai
        /// Get list user is waiting accept for project
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="projectid"></param>
        /// <returns></returns>
        public List<User> GetWaitingAcceptUserForProject(string ApplicationName, int projectid)
        {
            List<User> ret = null;

            try
            {
                UserManager um = new UserManager(ApplicationName);

                ret = um.GetWaitingAcceptUserForProject(projectid);
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Helper.GetWaitingAcceptUserForProject error {0}", ex);
            }

            return ret;
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "ahs_service")]
        /// <summary>
        /// Author: Hoang Van Tai
        /// Get the user list  in Project and organization 
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <returns></returns>
        public Dictionary<Guid, User> GetListUser(string ApplicationName)
        {
            Dictionary<Guid, User> ret = null;

            try
            {
                UserManager um = new UserManager(ApplicationName);

                ret = um.GetAllUsers();
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Helper.GetListUser error {0}", ex);
            }

            return ret;
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "ahs_service")]
        /// <summary>
        /// Author: Hoang Van Tai
        /// Get list user not in any Organization
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <returns></returns>
        /// Add:Hoang Van Tai
        /// Modify: Le Anh Phong, Hoang Van Tai
        public Dictionary<Guid, User> GetUsersNotInAnyOrgan(string ApplicationName)
        {
            Dictionary<Guid, User> ret = null;

            try
            {
                UserManager um = new UserManager(ApplicationName);

                ret = um.GetUsersNotInAnyOrgan();
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Helper.GetUsersNotInAnyOrgan error {0}", ex);
            }

            return ret;
        }

        /// <summary>
        /// Author: Hoang Van Tai
        /// GetUserInfo
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="username"></param>
        /// <returns></returns>
        /// Add:Hoang Van Tai
        /// Modify:
        public User GetUserInfo(string ApplicationName, string PermitName, string username)
        {
            // Get user information where  ApplicationName, username
            User ret = null;

            try
            {
                // Select user information where u.Applicationname= ApplicationName and u.Username=username
                // and joined AHSUserInProjects on u.Userid equals uip.Userid

                if (!string.IsNullOrEmpty(PermitName) && !string.IsNullOrEmpty(username))
                {
                    RoleManager rm = new RoleManager(ApplicationName);
                    UserManager um = new UserManager(ApplicationName);

                    // If user is PermitUser then 
                    if (rm.IsPermitUserForUser(PermitName, username))
                    {
                        ret = um.GetUserInfo(PermitName, username);
                    }
                    else
                    {
                        ret = um.GetUserInfo(username);
                    }
                    ret.IsPermitUser = (username.ToLower() == "sa");

                }
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Helper.GetUserInfo error {0}", ex);
            }

            return ret;
        }

        /// <summary>
        /// Author: Hoang Van Tai
        /// Check User isExits in the AHS, input username is username or email  
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="username"></param>
        /// <returns></returns>
        public bool IsExistUserInAHS(string ApplicationName, string username)
        {
            bool ret = false;

            try
            {
                UserManager um = new UserManager(ApplicationName);

                ret = um.IsExistUserInAHS(username);
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Helper.IsExistUserInAHS error {0}", ex);
            }

            return ret;
        }

        /// <summary>
        /// Author:Hoang Van Tai
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="username"></param>
        /// <returns></returns>
        public bool IsExistUser(string ApplicationName, string username)
        {
            bool ret = false;

            try
            {
                UserManager um = new UserManager(ApplicationName);

                ret = um.IsExistUser(username);
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Helper.IsExistUser error {0}", ex);
            }

            return ret;
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "ahs_service")]
        /// <summary>
        /// AddUserToProject
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="username"></param>
        /// <param name="projectid"></param>
        public void AddUserToProject(string ApplicationName, string username, int projectid)
        {
            try
            {
                UserManager um = new UserManager(ApplicationName);
                um.AddUserToProject(username, projectid); 
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Helper.AddUserToProject error {0}", ex);
            }
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "ahs_service")]
        /// <summary>
        /// InviteUsersToProject
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="userId"></param>
        /// <param name="projectID"></param>
        /// <returns></returns>
        public int InviteUsersToProject(string ApplicationName, string[] userId, int projectID)
        {
            int ret = 0;

            try
            {
                UserManager um = new UserManager(ApplicationName);
                ret = um.InviteUsersToProject(userId, projectID);
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Helper.InviteUsersToProject error {0}", ex);
                ret = -1;
            }

            return ret;
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "ahs_service")]
        /// <summary>
        /// AcceptInvitedUserToProject
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="userId"></param>
        /// <param name="projectId"></param>
        /// <param name="isAccept"></param>
        /// <returns></returns>
        public int AcceptInvitedUserToProject(string ApplicationName, string userId, int projectId, bool isAccept)
        {
            int ret = 0;

            try
            {
                UserManager um = new UserManager(ApplicationName);
                ret = um.AcceptInvitedUserToProject(userId, projectId, isAccept);
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Helper.AcceptInvitedUserToProject error {0}", ex);
                ret = -1;
            }

            return ret;
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "ahs_service")]
        /// <summary>
        /// AddUserToProjectIncludeRole
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="username"></param>
        /// <param name="projectid"></param>
        /// <param name="roleName"></param>
        public bool AddUserToProjectIncludeRole(string ApplicationName, string username, int projectid, string roleName)
        {
            try
            {
                UserManager um = new UserManager(ApplicationName);

                return um.AddUserToProject(username, projectid, roleName);
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Helper.AddUserToProjectIncludeRole error {0}", ex);
                return false;
            }
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "ahs_service")]
        /// <summary>
        /// GetOrganization
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <returns></returns>
        public Dictionary<Guid, Organization> GetOrganization(string ApplicationName)
        {
            Dictionary<Guid, Organization> ret = null;

            try
            {
                OrganizationManager om = new OrganizationManager(ApplicationName);

                ret = om.GetOrganization();
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Helper.GetOrganization error {0}", ex);
            }

            return ret;
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "ahs_service")]
        /// <summary>
        /// Get Organization By PrimaryKey
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="OrgId"></param>
        /// <param name="msgError"></param>
        /// <returns></returns>
        public Organization GetOrganizationById(string ApplicationName, string OrgId, ref string msgError)
        {
            try
            {
                OrganizationManager om = new OrganizationManager(ApplicationName);

                return om.GetById(OrgId, ref msgError);
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Helper.GetOrganizationById code {0}", ex);

                return null;
            }
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "ahs_service")]
        /// <summary>
        /// GetUserInOrganization
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="OrgId"></param>
        /// <returns></returns>
        public Dictionary<Guid, MembershipUser> GetUserInOrganization(string ApplicationName, Guid OrgId)
        {
            Dictionary<Guid, MembershipUser> ret = null;

            try
            {
                UserManager um = new UserManager(ApplicationName);

                ret = um.GetUserInOrganization(OrgId);
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Helper.GetUserInOrganization error {0}", ex);
            }

            return ret;
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "ahs_service")]
        /// <summary>
        /// GetProjectForOrganization
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="OrgId"></param>
        /// <returns></returns>
        public Dictionary<int, Project> GetProjectForOrganization(string ApplicationName, Guid OrgId)
        {
            Dictionary<int, Project> ret = null;

            try
            {
                ProjectManager pm = new ProjectManager(ApplicationName);

                ret = pm.GetProjectForOrganization(OrgId);
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Helper.GetProjectForOrganization error {0}", ex);
            }

            return ret;
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "ahs_service")]
        /// <summary>
        /// Add user to a orgnization list
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="UserID"></param>
        /// <param name="idOrgs"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public bool AddUserToOrgs(string ApplicationName, Guid UserID, string[] idOrgs, ref string msg)
        {
            bool ret = true;

            try
            {
                UserManager um = new UserManager(ApplicationName);

                ret = um.AddUserToOrgs(UserID, idOrgs, ref msg);
            }
            catch (Exception ex)
            {
                msg = ex.Message;
                ret = false;

                Logger.Log.ErrorFormat("[" + ApplicationName + "] Helper.AddUserToOrgs error {0}", ex);
            }

            return ret;
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "ahs_service")]
        /// <summary>
        /// Add user to a project list
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="UserID"></param>
        /// <param name="idProjs"></param>
        /// <returns></returns>
        public bool AddUserToProjects(string ApplicationName, Guid UserID, string[] idProjs)
        {
            bool ret = true;

            try
            {
                UserManager um = new UserManager(ApplicationName);

                ret = um.AddUserToProjects(UserID, idProjs);
            }
            catch (Exception ex)
            {
                ret = false;

                Logger.Log.ErrorFormat("[" + ApplicationName + "] Helper.AddUserToProject error {0}", ex);
            }

            return ret;
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "ahs_service")]
        /// <summary>
        /// Remove user from all project
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="UserID"></param>
        /// <returns></returns>
        public bool RemoveUserFromProjects(string ApplicationName, Guid UserID)
        {
            bool ret = true;

            try
            {
                UserManager um = new UserManager(ApplicationName);

                ret = um.RemoveUserFromProjects(UserID);
            }
            catch (Exception ex)
            {
                ret = false;

                Logger.Log.ErrorFormat("[" + ApplicationName + "] Helper.RemoveUserFromProjects error {0}", ex);
            }

            return ret;
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "ahs_service")]
        /// <summary>
        /// Author:Hoang Van Tai
        /// Remove user from project for project, role
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="projectId"></param>
        /// <param name="username"></param>
        /// <param name="rolename"></param>
        /// <returns></returns>
        public bool RemoveUserFromProject(string ApplicationName, int projectId, string username, string rolename = null)
        {
            bool ret = true;

            try
            {
                UserManager um = new UserManager(ApplicationName);

                ret = um.RemoveUserFromProjects(projectId, username, rolename);
            }
            catch (Exception ex)
            {
                ret = false;

                Logger.Log.ErrorFormat("[" + ApplicationName + "] Helper.RemoveUserFromProject error {0}", ex);
            }

            return ret;
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "ahs_service")]
        /// <summary>
        /// Remove user from all organization
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="UserID"></param>
        /// <returns></returns>
        public bool RemoveUserFromOrgs(string ApplicationName, Guid UserID)
        {
            bool ret = true;

            try
            {
                UserManager um = new UserManager(ApplicationName);

                ret = um.RemoveUserFromOrgs(UserID);
            }
            catch (Exception ex)
            {
                ret = false;

                Logger.Log.ErrorFormat("[" + ApplicationName + "] Helper.RemoveUserFromOrgs error {0}", ex);
            }

            return ret;
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "ahs_service")]
        /// <summary>
        /// Author: Hoang Van Tai
        /// Remove User From Project in list idPrjDels for ApplicationName 
        /// </summary>
        /// <param name="UserID"></param>
        /// <param name="idPrjDels"></param>
        /// <returns></returns>        
        public bool RemoveUserFromProjectsForApplication(string ApplicationName, Guid UserID, Int32[] idPrjDels, ref string message)
        {
            bool ret = false;

            try
            {
                UserManager um = new UserManager(ApplicationName);

                ret = um.RemoveUserFromProjectsForApplication(UserID, idPrjDels, ref message);
            }
            catch (Exception ex)
            {
                message = ex.Message;

                Logger.Log.ErrorFormat("[" + ApplicationName + "] Helper.RemoveUserFromProjectsForApplication error {0}", ex);
            }

            return ret;
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "ahs_service")]
        /// <summary>
        /// Author: Hoang Van Tai
        /// Remove User From Oranization in list idOrgDels for ApplicationName 
        /// </summary>
        /// <param name="UserID"></param>
        /// <param name="idOrgDels"></param>
        /// <returns></returns>        
        public bool RemoveUserFromOrgsForApplication(string ApplicationName, Guid UserID, string[] idOrgDels, ref string message)
        {
            bool ret = false;

            try
            {
                UserManager um = new UserManager(ApplicationName);

                ret = um.RemoveUserFromOrgsForApplication(UserID, idOrgDels, ref message);
            }
            catch (Exception ex)
            {
                message = ex.Message;

                Logger.Log.ErrorFormat("[" + ApplicationName + "] Helper.RemoveUserFromOrgsForApplication error {0}", ex);
            }

            return ret;
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "ahs_service")]
        /// <summary>
        /// Author: Hoang Van Tai
        /// Delete the project for ApplicationName 
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="ProjectID"></param>
        /// <returns></returns>        
        public bool DeleteProject(string ApplicationName, int ProjectID)
        {
            bool ret = false;

            try
            {
                ProjectManager pm = new ProjectManager(ApplicationName);

                ret = pm.DeleteProject(ProjectID);
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Helper.DeleteProject error {0}", ex);
            }

            return ret;
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "ahs_service")]
        /// <summary>
        /// Get role list for user
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="username"></param>
        /// <returns></returns>
        public Dictionary<Guid, Role> GetRolesForUser2(string ApplicationName, string username)
        {
            Dictionary<Guid, Role> ret = null;

            try
            {
                RoleManager rm = new RoleManager(ApplicationName);

                ret = rm.GetRolesForUser2(username);
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Helper.GetRolesForUser2 error {0}", ex);
            }

            return ret;
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "ahs_service")]
        /// <summary>
        /// Add Organization
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="name"></param>
        /// <param name="description"></param>
        /// <param name="idUsers"></param>
        /// <param name="idPrjRoles"></param>
        /// <param name="OwnerID"></param>
        /// <returns></returns>
        public bool AddOrganization(string ApplicationName, string name, string description, string[] idUsers, string[] idPrjRoles, Guid OwnerID)
        {
            bool ret = true;

            try
            {
                OrganizationManager om = new OrganizationManager(ApplicationName);

                ret = om.AddOrganization(name, description, idUsers, idPrjRoles, OwnerID);
            }
            catch (Exception ex)
            {
                ret = false;

                Logger.Log.ErrorFormat("[" + ApplicationName + "] Helper.AddOrganization error {0}", ex);
            }

            return ret;
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "ahs_service")]
        /// <summary>
        /// Get organization list for user
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="userID"></param>
        /// <returns></returns>
        public Dictionary<Guid, Organization> GetOrganizationForUser(string ApplicationName, Guid userID)
        {
            Dictionary<Guid, Organization> ret = null;

            try
            {
                OrganizationManager om = new OrganizationManager(ApplicationName);

                ret = om.GetOrganizationForUser(userID);
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Helper.GetOrganizationForUser error {0}", ex);
            }

            return ret;
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "ahs_service")]
        /// <summary>
        /// Get organization list for project
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="projectID"></param>
        /// <returns></returns>
        public Dictionary<Guid, Organization> GetOrganizationForProject(string ApplicationName, int projectID)
        {
            Dictionary<Guid, Organization> ret = null;

            try
            {
                OrganizationManager om = new OrganizationManager(ApplicationName);

                ret = om.GetOrganizationForProject(projectID);
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Helper.GetOrganizationForProject error {0}", ex);
            }

            return ret;
        }

        /// <summary>
        /// Author: Hoang Van Tai
        /// GetOrganizationsForProject
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="projectID"></param>
        /// <param name="rolename"></param>
        /// <returns></returns>
        public List<Organization> GetOrganizationsForProject(string ApplicationName, int projectID, string rolename)
        {
            List<Organization> ret = null;
            try
            {
                OrganizationManager om = new OrganizationManager(ApplicationName);
                ret = om.GetOrganizationForProject(projectID, rolename);   
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Helper.GetOrganizationsForProject error {0}", ex);
            }

            return ret;
        }
        //[PrincipalPermission(SecurityAction.Demand, Role = "ahs_service")]
        /// <summary>
        /// Author: Hoang Van Tai
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="OrgID"></param>
        /// <param name="name"></param>
        /// <param name="description"></param>
        /// <param name="idUsers"></param>
        /// <param name="idPrjRoles"></param>
        /// <returns></returns>        
        public bool UpdateOrganization(string ApplicationName, Guid OrgID, string name, string description, string[] idUsers, string[] idPrjRoles)
        {
            bool ret = false;

            try
            {
                OrganizationManager om = new OrganizationManager(ApplicationName);

                ret = om.UpdateOrganization(OrgID, name, description, idUsers, idPrjRoles);
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Helper.UpdateOrganization error {0}", ex);
            }

            return ret;
        }

        /// <summary>
        /// Author: Hoang Van Tai
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="OrgID"></param>
        /// <param name="name"></param>
        /// <param name="description"></param>
        /// <param name="idUsers"></param>
        /// <returns></returns>
        public bool UpdateOrganizationNotProject(string ApplicationName, Guid OrgID, string name, string description, string[] idUsers)
        {
            bool ret = false;

            try
            {
                OrganizationManager om = new OrganizationManager(ApplicationName);

                ret = om.UpdateOrganization(OrgID, name, description, idUsers);
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Helper.UpdateOrganizationNotProject error {0}", ex);
            }

            return ret;
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "ahs_service")]
        /// <summary>
        /// Delete organization
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="OrgID"></param>
        /// <returns></returns>
        public bool DeleteOrganization(string ApplicationName, Guid OrgID)
        {
            bool ret = false;

            try
            {
                OrganizationManager om = new OrganizationManager(ApplicationName);

                ret = om.DeleteOrganization(OrgID);
            }
            catch (Exception ex)
            {
                ret = false;

                Logger.Log.ErrorFormat("[" + ApplicationName + "] Helper.DeleteOrganization error {0}", ex);
            }

            return ret;
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "ahs_service")]
        /// <summary>
        /// Remove all organization from project
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="ProjectID"></param>
        /// <returns></returns>
        public bool RemoveOrgsFromProject(string ApplicationName, int ProjectID)
        {
            bool ret = false;

            try
            {
                OrganizationManager om = new OrganizationManager(ApplicationName);

                ret = om.RemoveOrgsFromProject(ProjectID);
            }
            catch (Exception ex)
            {
                ret = false;

                Logger.Log.ErrorFormat("[" + ApplicationName + "] Helper.RemoveOrgsFromProject error {0}", ex);
            }

            return ret;
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "ahs_service")]
        /// <summary>
        /// Add a organization list into project
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="ProjectID"></param>
        /// <param name="idOrgs"></param>
        /// <returns></returns>
        public bool AddOrgsToProject(string ApplicationName, int ProjectID, string[] idOrgs)
        {
            bool ret = false;

            try
            {
                OrganizationManager om = new OrganizationManager(ApplicationName);

                ret = om.AddOrgsToProject(ProjectID, idOrgs);
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Helper.AddOrgsToProject error {0}", ex);
            }

            return ret;
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "ahs_service")]
        /// <summary>
        /// Add project
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="name"></param>
        /// <param name="description"></param>
        /// <param name="owner"></param>
        /// <param name="idUserRoles"></param>
        /// <param name="idOrgRoles"></param>
        /// <returns></returns>
        public int AddProject(string ApplicationName, string name, string description, string owner, string[] idUserRoles, string[] idOrgRoles)
        {
            try
            {
                ProjectManager pm = new ProjectManager(ApplicationName);

                return pm.AddProject(name, description, owner, idUserRoles, idOrgRoles);
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Helper.AddProject error {0}", ex);

                return -1;
            }
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "ahs_service")]
        /// <summary>
        /// Update project
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="mainPrjId"></param>
        /// <param name="name"></param>
        /// <param name="description"></param>
        /// <param name="owner"></param>
        /// <param name="idUserRoles"></param>
        /// <param name="idOrgRoles"></param>
        /// <returns></returns>
        public int UpdateProject(string ApplicationName, int mainPrjId, string name, string description, string owner, string[] idUserRoles, string[] idOrgRoles)
        {
            try
            {
                ProjectManager pm = new ProjectManager(ApplicationName);

                return pm.UpdateProject(mainPrjId, name, description, owner, idUserRoles, idOrgRoles);
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Helper.UpdateProject error {0}", ex);

                return -1;
            }
        }

        /// <summary>
        /// Send mail from ahs
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="userName"></param>
        /// <param name="email"></param>
        /// <param name="subject"></param>
        /// <param name="body"></param>
        /// <returns></returns>
        public bool SendMail(string ApplicationName, string userName, string email, string subject, string body)
        {
            try
            {
                NotificationUtils.SendMail(userName, email, subject, body, MailType.ExternalNotification);

                return true;
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Helper.SendMail error {0}", ex);

                return false;
            }
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "ahs_service")]
        /// <summary>
        /// Send a request for forgot password of member
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="userName"></param>
        /// <returns></returns>
        public bool SendRequestForgotPassword(string ApplicationName, string userName, string linkReset)
        {
            try
            {
                //Check user is existing in application
                UserManager um = new UserManager(ApplicationName);
                ForgotValidateManager fvm = new ForgotValidateManager(ApplicationName);
                ApplicationLinkManager alm = new ApplicationLinkManager(ApplicationName);

                if (um.IsExistUser(userName))
                {
                    // Is exists => Send request to email of user
                    User user = um.GetUserInfo(userName);

                    //Send code reset password to email
                    var newPass = CSUtils.RandomStr(5);
                    var code = Guid.NewGuid().ToString().Replace("-", "");

                    string link = alm.GetLinkByApplication() + linkReset + "?code=" + code;
                    string body = "<p>Hi " + user.UserName + ", you can now <a href='" + link + "'>reset</a> your password.</p>";
                    string message = string.Empty;

                    if (NotificationUtils.SendMail("AHS Admin", user.Email, "Password reset", body, MailType.ExternalNotification))
                    {
                        fvm.InsertForgotPass(code, userName, newPass);
                    }

                    return true;
                }

                return false;
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Helper.SendRequestForgotPassword error {0}", ex);

                return false;
            }
        }

        /// <summary>
        /// Check forgotcode is existing
        /// </summary>
        /// <returns></returns>
        public bool IsExistForgotCode(string ApplicationName, string code)
        {
            try
            {
                return new ForgotValidateManager(ApplicationName).GetForgotPass(code) != null;
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName  + "] Helper.IsExistForgotCode error {0}", ex);

                return false;
            }
        }

        /// <summary>
        /// Change password by code
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="code"></param>
        /// <param name="passWord"></param>
        /// <returns></returns>
        public bool ChangePassByCode(string ApplicationName, string code, string passWord)
        {
            try
            {
                ForgotValidateManager fvm = new ForgotValidateManager(ApplicationName);
                UserManager um = new UserManager(ApplicationName);

                var entity = fvm.GetForgotPass(code);

                if (entity != null)
                {
                    // Validation user existing in application
                    MembershipUser mu = um.GetUser(entity.Username, true);

                    if (mu != null)
                    {
                        // User existing => Reset pass
                        string pass_reset = mu.ResetPassword();

                        // Changepass
                        mu.ChangePassword(pass_reset, passWord);

                        // Delete all validate forgot
                        fvm.DeleteAllForgotByUser(mu.UserName);

                        return true;
                    }
                }

                return false;
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Helper.ChangePassByCode code {0}", ex);

                return false;
            }
        }

        /// <summary>
        /// Add role for project type
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="ProjectTypeName"></param>
        /// <param name="RoleName"></param>
        /// <returns></returns>
        public bool AddRoleForProjectType(string ApplicationName, string ProjectTypeName, string RoleName, string RoleDisplay, int? Priority = null)
        {
            try
            {
                RoleManager rm = new RoleManager(ApplicationName);

                return rm.AddRoleForProjectType(ProjectTypeName, RoleName, RoleDisplay, Priority);
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Helper.AddRoleForProjectType code {0}", ex);

                return false;
            }
        }

        /// <summary>
        /// Get roles for Project Type
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="ProjectTypeName"></param>
        /// <returns></returns>
        public string[] GetAllRolesForProjectType(string ApplicationName, string ProjectTypeName)
        {
            try
            {
                RoleManager rm = new RoleManager(ApplicationName);

                return rm.GetAllRolesForProjectType(ProjectTypeName).ToArray();
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Helper.GetAllRolesForProjectType code {0}", ex);

                return null;
            }
        }
        
        #endregion

        #region "Org - SA"

        public List<OrgInProject> GetProjectInvitedOrganization(string ApplicationName,Guid orgID, ref string msgError)
        {
            try
            {
                OrganizationManager om = new OrganizationManager(ApplicationName);

                return om.GetProjectInvitedOrganization(orgID, ref msgError);
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Helper.GetProjectInvitedOrganization code {0}", ex);

                return null;
            }
        }

        public int ChangeStatusOrganInProject(string ApplicationName, Guid orgID, int prjID, int status, ref string msgError)
        {
            try
            {
                OrganizationManager om = new OrganizationManager(ApplicationName);

                return om.ChangeStatusOrganInProject(orgID, prjID, status, ref msgError);
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Helper.ChangeStatusOrganInProject code {0}", ex);

                return -1;
            }
        }

        /// <summary>
        /// Author: Hoang Van Tai
        /// Search Organization by SA
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="OrgId"></param>
        /// <param name="msgError"></param>
        /// <returns></returns>
        public List<Organization> SearchOrganizationSA(string ApplicationName, string OrgId, ref string msgError)
        {
            try
            {
                OrganizationManager om = new OrganizationManager(ApplicationName);

                return om.SearchOrganizationSA(OrgId, ref msgError);

            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Helper.SearchOrganizationSA code {0}", ex);

                return null;
            }
        }

        /// <summary>
        /// Author: Hoang Van Tai
        /// add organization properties All
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="name"></param>
        /// <param name="description"></param>
        /// <param name="organtype"></param>
        /// <param name="orgrole"></param>
        /// <param name="ownerid"></param>
        /// <param name="ownertype"></param>
        /// <param name="userInvite"></param>
        /// <param name="projecttype"></param>
        /// <param name="userNames"></param>
        /// <param name="idPrjRoles"></param>
        /// <param name="userRoles"></param>
        /// <param name="msgError"></param>
        /// <returns></returns>
        public Guid? AddOrganizationPropertiesAll(string ApplicationName, string name, string description, string organtype, string orgrole, string ownerid, string ownertype, string userInvite, string projecttype, string[] userNames, string[] idPrjRoles, string[] userRoles, ref string msgError)
        {
            try
            {
                OrganizationManager om = new OrganizationManager(ApplicationName);

                return om.AddOrganization(name, description, organtype, orgrole, ownerid, ownertype, userInvite, projecttype, userNames, idPrjRoles, userRoles, ref msgError); 

            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Helper.GetAllRolesForProjectType code {0}", ex);

                return null;
            }
        }

        /// <summary>
        /// Author: Hoang Van Tai
        /// Update Organization Properties All
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="orgId"></param>
        /// <param name="name"></param>
        /// <param name="description"></param>
        /// <param name="organtype"></param>
        /// <param name="orgrole"></param>
        /// <param name="ownerid"></param>
        /// <param name="ownertype"></param>
        /// <param name="userInvite"></param>
        /// <param name="projecttype"></param>
        /// <param name="idUsers"></param>
        /// <param name="idPrjRoles"></param>
        /// <param name="msgError"></param>
        /// <returns></returns>
        public int UpdateOrganizationPropertiesAll(string ApplicationName,string orgId, string name, string description, string organtype, string orgrole, string ownerid, string ownertype, string userInvite, string projecttype, string[] idUsers, string[] idPrjRoles, ref string msgError)
        {
            try
            {
                OrganizationManager om = new OrganizationManager(ApplicationName);

                return om.UpdateOrganization(orgId, name, description, organtype, orgrole, ownerid, ownertype, userInvite, projecttype, idUsers, idPrjRoles, ref msgError);
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Helper.UpdateOrganizationPropertiesAll code {0}", ex);

                return -1;
            }
        }

        /// <summary>
        /// Author: Hoang Van Tai
        /// Assign list user join to project
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="orgId"></param>
        /// <param name="username"></param>
        /// <param name="userNames"></param>
        /// <param name="msgError"></param>
        /// <returns></returns>
        public int AssignUsersToOrg(string ApplicationName, Guid orgId, string username, string[] userNames, ref string msgError)
        {
            try
            {
                OrganizationManager om = new OrganizationManager(ApplicationName);

                return om.AssignUsersToOrg(orgId, username, userNames, ref msgError);
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Helper.AssignUsersToOrg code {0}", ex);

                return -1;
            }
        }

        /// <summary>
        /// Author: Hoang Van Tai
        /// Save user and role to Organization
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="orgId"></param>
        /// <param name="userInvite"></param>
        /// <param name="userRoles"></param>
        /// <param name="msgError"></param>
        /// <returns></returns>
        public int SaveUsersRoleToOrg(string ApplicationName, Guid orgId, string userInvite, string[] userRoles, ref string msgError)
        {
            try
            {
                OrganizationManager om = new OrganizationManager(ApplicationName);

                return om.SaveUsersRoleToOrg(orgId, userInvite, userRoles, ref msgError);

            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Helper.SaveUsersRoleToOrg code {0}", ex);

                return -1;
            }
        }

        /// <summary>
        /// Author: Hoang Van Tai
        /// Remove user from Organization
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="idUserDels"></param>
        /// <param name="orgId"></param>
        /// <param name="msgError"></param>
        /// <returns></returns>
        public int RemoveUsersFromOrg(string ApplicationName, string[] idUserDels, Guid orgId, ref string msgError)
        {
            try
            {
                OrganizationManager om = new OrganizationManager(ApplicationName);

                return om.RemoveUsersFromOrg(idUserDels, orgId, ref msgError);
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Helper.RemoveUsersFromOrg code {0}", ex);

                return -1;
            }
        }

        /// <summary>
        /// Author: Hoang Van Tai
        /// Get list organization from user
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="userId"></param>
        /// <param name="msgError"></param>
        /// <returns></returns>
        public List<Organization> GetOrgsListFromUser(string ApplicationName, string userId, ref string msgError)
        {
            try
            {
                OrganizationManager om = new OrganizationManager(ApplicationName);

                return om.GetOrgsListFromUser(userId, ref msgError);

            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Helper.GetOrgsListFromUser code {0}", ex);

                return null;
            }
        }

        /// <summary>
        /// Author: Hoang Van Tai
        /// Get list organization for not orgName
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="orgName"></param>
        /// <param name="msgError"></param>
        /// <returns></returns>
        public List<Organization> GetOrganizationsListNotOrgName(string ApplicationName, string orgName, ref string msgError)
        {
            try
            {
                OrganizationManager om = new OrganizationManager(ApplicationName);

                return om.GetOrganizationsListNotOrgName(orgName, ref msgError);
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Helper.GetOrganizationsListNotOrgName code {0}", ex);

                return null;
            }
        }

        /// <summary>
        /// Author: Hoang Van Tai
        /// Save Org and Role to Project
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="orgId"></param>
        /// <param name="userInvite"></param>
        /// <param name="prjRoles"></param>
        /// <param name="msgError"></param>
        /// <returns></returns>
        public int SaveOrgsRoleToPrj(string ApplicationName, Guid orgId, string userInvite, string[] prjRoles, ref string msgError)
        {
            try
            {
                OrganizationManager om = new OrganizationManager(ApplicationName);

                return om.SaveOrgsRoleToPrj(orgId, userInvite, prjRoles, ref msgError);

            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Helper.SaveOrgsRoleToPrj code {0}", ex);

                return -1;
            }
        }

        /// <summary>
        /// Author: Hoang Van Tai
        /// Add users to Org 
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="orgId"></param>
        /// <param name="userInvite"></param>
        /// <param name="userNames"></param>
        /// <param name="msgError"></param>
        /// <returns></returns>
        public int AddUsersToOrg(string ApplicationName, Guid orgId, string userInvite, string[] userNames, ref string msgError)
        {
            try
            {
                OrganizationManager om = new OrganizationManager(ApplicationName);

                return om.AddUsersToOrg(orgId, userInvite, userNames, ref msgError);

            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Helper.SaveOrgsRoleToPrj code {0}", ex);

                return -1;
            }
        }

        /// <summary>
        /// Author: Hoang Van Tai
        /// Remove list project from org
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="idPrjDels"></param>
        /// <param name="orgId"></param>
        /// <returns></returns>
        public int RemoveProjectsFromOrg(string ApplicationName, string[] idPrjDels, Guid orgId)
        {
            try
            {
                OrganizationManager om = new OrganizationManager(ApplicationName);

                return om.RemoveProjectsFromOrg( idPrjDels, orgId);

            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Helper.RemoveProjectsFromOrg code {0}", ex);

                return -1;
            }
        }

        /// <summary>
        /// Author: Hoang Van Tai
        /// Save Prj and Role to Org
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="orgId"></param>
        /// <param name="userInvite"></param>
        /// <param name="idPrjDels"></param>
        /// <param name="PrjRoles"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public int SavePrjsRoleToOrg(string ApplicationName, string orgId, string userInvite, string[] idPrjDels, string[] PrjRoles, ref string message)
        {
            try
            {
                OrganizationManager om = new OrganizationManager(ApplicationName);

                return om.SavePrjsRoleToOrg(orgId, userInvite, idPrjDels, PrjRoles, ref message);

            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Helper.SavePrjsRoleToOrg code {0}", ex);

                return -1;
            }
        }

        /// <summary>
        /// Author: Hoang Van Tai
        /// Get List project from Organization
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="orgId"></param>
        /// <param name="msgError"></param>
        /// <returns></returns>
        public List<OrgInProject> GetProjectListFromOrganization(string ApplicationName, string orgId, ref string msgError)
        {
            try
            {
                OrganizationManager om = new OrganizationManager(ApplicationName);

                return om.GetProjectListFromOrganization(orgId, ref msgError);
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Helper.GetProjectListFromOrganization code {0}", ex);

                return null;
            }
        }

        /// <summary>
        /// Author: Hoang Van Tai
        /// Get List Organization by SA
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="msgError"></param>
        /// <returns></returns>
        public List<Organization> GetOrganizationsListBySA(string ApplicationName, ref string msgError)
        {
            try
            {
                OrganizationManager om = new OrganizationManager(ApplicationName);

                return om.GetOrganizationsListBySA(ref msgError);
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Helper.GetOrganizationsListBySA code {0}", ex);

                return null ;
            }
        }

        /// <summary>
        /// Author: Hoang Van Tai
        /// Get List Organization and User 
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public List<Organization> GetOwnersList(string ApplicationName, string user)
        {
            try
            {
                OrganizationManager om = new OrganizationManager(ApplicationName);

                return om.GetOwnersList(user);
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Helper.GetOwnersList code {0}", ex);

                return null;
            }
        }
        #endregion

        #region "User - SA"
        /// <summary>
        /// Author: Hoang Van Tai
        /// Insert Invite Email Org
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="emails"></param>
        /// <param name="OrgId"></param>
        /// <param name="RoleId"></param>
        /// <returns></returns>
        public string InsertInviteEmailOrg(string ApplicationName, string emails, Guid OrgId, Guid? RoleId)
        {
            try
            {
                UserManager um = new UserManager(ApplicationName);

                return um.InsertInviteEmailOrg(emails, OrgId, RoleId);
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Helper.GetAllRolesForProjectType code {0}", ex);

                return null;
            }
        }

        /// <summary>
        /// Author: Hoang Van Tai
        /// Insert Invite
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="emails"></param>
        /// <param name="RoleId"></param>
        /// <param name="OwnerID"></param>
        /// <returns></returns>
        public string InsertInvite(string ApplicationName, string emails, Guid? RoleId, Guid? OwnerID)
        {
            try
            {
                UserManager um = new UserManager(ApplicationName);

                return um.InsertInvite(emails, RoleId, OwnerID);
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Helper.InsertInvite code {0}", ex);

                return null;
            }
        }

        /// <summary>
        /// Author: Hoang Van Tai
        /// Get InviteOrganization by SA
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="userId"></param>
        /// <param name="msgError"></param>
        /// <returns></returns>
        public List<UserInOrganization> GetInviteOrganizationsListBySA(string ApplicationName, string userId, ref string msgError)
        {
            try
            {
                UserManager um = new UserManager(ApplicationName);

                return um.GetInviteOrganizationsListBySA(userId, ref msgError);

            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Helper.GetInviteOrganizationsListBySA code {0}", ex);

                return null;
            }
        }

        /// <summary>
        /// Author: Hoang Van Tai
        /// add org to user
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="userId"></param>
        /// <param name="userInviteID"></param>
        /// <param name="idOrgs"></param>
        /// <param name="msgError"></param>
        /// <returns></returns>
        public int AddOrgsToUser(string ApplicationName, Guid userId, string userInviteID, string[] idOrgs, ref string msgError)
        {
            try
            {
                UserManager um = new UserManager(ApplicationName);

                return um.AddOrgsToUser(userId, userInviteID, idOrgs, ref msgError);
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Helper.AddOrgsToUser code {0}", ex);

                return -1;
            }
        }

        /// <summary>
        /// Author: Hoang Van Tai
        /// Get list user from Org
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="orgId"></param>
        /// <param name="msgError"></param>
        /// <returns></returns>
        public List<User> GetUsersListFromOrg(string ApplicationName, string orgId, ref string msgError)
        {
            try
            {
                UserManager um = new UserManager(ApplicationName);

                return um.GetUsersListFromOrg(orgId, ref msgError);
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Helper.GetUsersListFromOrg code {0}", ex);

                return null;
            }
        }

        /// <summary>
        /// Author: Hoang Van Tai
        /// Set Approved for user
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="userId"></param>
        /// <param name="isApproved"></param>
        /// <param name="msgError"></param>
        /// <returns></returns>
        public int SetApproved(string ApplicationName, Guid userId, bool isApproved, ref string msgError)
        {
            try
            {
                UserManager um = new UserManager(ApplicationName);

                return um.SetApproved(userId, isApproved, ref msgError);
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Helper.SetApproved code {0}", ex);

                return -1;
            }
        }

        /// <summary>
        /// Author: Hoang Van Tai
        /// Get user owner for userId
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="userId"></param>
        /// <param name="msgError"></param>
        /// <returns></returns>
        public User GetUserOwnerByPrimaryKey(string ApplicationName, string userId, ref string msgError)
        {
            try
            {
                UserManager um = new UserManager(ApplicationName);

                return um.GetUserOwnerByPrimaryKey(userId, ref msgError);
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Helper.GetUserOwnerByPrimaryKey code {0}", ex);

                return null ;
            }
        }

        /// <summary>
        /// Author: Hoang Van Tai
        /// Action invite list organization
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="userId"></param>
        /// <param name="orgId"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        public bool ActionInviteOrganizationsList(string ApplicationName, string userId, string orgId, int status)
        {
            try
            {
                UserManager um = new UserManager(ApplicationName);

                return um.ActionInviteOrganizationsList(userId, orgId, status);
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Helper.ActionInviteOrganizationsList code {0}", ex);

                return false;
            }
        }

        /// <summary>
        /// Author: Hoang Van Tai
        /// Action invite list project
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="userId"></param>
        /// <param name="prjId"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        public bool ActionInviteProjectsList(string ApplicationName, string userId, int prjId, int status)
        {
            try
            {
                UserManager um = new UserManager(ApplicationName);

                return um.ActionInviteProjectsList(userId, prjId, status);
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Helper.AddOrgsToUser code {0}", ex);

                return false;
            }
        }

        /// <summary>
        /// Author: Hoang Van Tai
        /// Get list project invite user
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="userId"></param>
        /// <param name="msgError"></param>
        /// <returns></returns>
        public List<UserInProject> GetInviteProjectsListBySA(string ApplicationName, string userId, ref string msgError)
        {
            try
            {
                UserManager um = new UserManager(ApplicationName);

                return um.GetInviteProjectsListBySA(userId, ref msgError);
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Helper.GetInviteProjectsListBySA code {0}", ex);

                return null ;
            }
        }

        /// <summary>
        /// Author: Hoang Van Tai
        /// Search user by SA
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="keyword"></param>
        /// <param name="msgError"></param>
        /// <returns></returns>
        public List<User> SearchUserSA(string ApplicationName, string keyword, ref string msgError)
        {
            try
            {
                UserManager um = new UserManager(ApplicationName);

                return um.SearchUserSA(keyword, ref msgError);
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Helper.SearchUserSA code {0}", ex);

                return null ;
            }
        }

        /// <summary>
        /// Author: Hoang Van Tai
        /// Get list user IsApproved or IsLockedOut
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <returns></returns>
        public List<User> GetListUserSA(string ApplicationName)
        {
            try
            {
                UserManager um = new UserManager(ApplicationName);

                return um.GetListUser();
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Helper.GetListUser code {0}", ex);

                return null;
            }
        }

        /// <summary>
        /// Author: Hoang Van Tai
        /// Update Prj and Role
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="userId"></param>
        /// <param name="idPrjRoles"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public int UpdatePrjAndRole(string ApplicationName, Guid userId, string[] idPrjRoles, ref string message)
        {
            try
            {
                UserManager um = new UserManager(ApplicationName);

                return um.UpdatePrjAndRole(userId, idPrjRoles, ref message);
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Helper.UpdatePrjAndRole code {0}", ex);

                return -1;
            }
        }

        /// <summary>
        /// Author: Hoang Van Tai
        /// Add user
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="user"></param>
        /// <param name="username"></param>
        /// <param name="name"></param>
        /// <param name="email"></param>
        /// <param name="phone"></param>
        /// <param name="password"></param>
        /// <param name="ownerid"></param>
        /// <param name="isApproved"></param>
        /// <param name="isSendEmail"></param>
        /// <param name="idOrgs"></param>
        /// <param name="idPrjRoles"></param>
        /// <param name="msgError"></param>
        /// <returns></returns>
        public Guid? AddUser(string ApplicationName, string user, string username, string name, string email, string phone, string password, string ownerid, string isApproved, string isSendEmail, string[] idOrgs, string[] idPrjRoles, ref string msgError)
        {
            try
            {
                UserManager um = new UserManager(ApplicationName);

                return um.AddUser(user, username, name, email, phone, password, ownerid, isApproved, isSendEmail, idOrgs, idPrjRoles, ref msgError);
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Helper.AddUser code {0}", ex);

                return null ;
            }
        }

        /// <summary>
        /// Update user Properties all
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="user"></param>
        /// <param name="userId"></param>
        /// <param name="name"></param>
        /// <param name="phone"></param>
        /// <param name="password"></param>
        /// <param name="email"></param>
        /// <param name="ownerid"></param>
        /// <param name="isApproved"></param>
        /// <param name="idOrgs"></param>
        /// <param name="idOrgDels"></param>
        /// <param name="idPrjRoles"></param>
        /// <param name="msgError"></param>
        /// <returns></returns>
        public int UpdateUserProperiesAll(string ApplicationName, string user, string userId, string name, string phone, string password, string email, string ownerid, string isApproved, string[] idOrgs, string[] idOrgDels, string[] idPrjRoles, ref string msgError)
        {
            try
            {
                UserManager um = new UserManager(ApplicationName);

                return um.UpdateUser(user, userId, name, phone, password, email, ownerid, isApproved,  idOrgs,  idOrgDels,  idPrjRoles, ref msgError);
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Helper.UpdateUser code {0}", ex);

                return -1;
            }
        }
        
        /// <summary>
        /// Author: Hoang Van Tai
        /// Remove Orgs from user 
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="idOrgDels"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        public int RemoveOrgsFromUser(string ApplicationName, string[] idOrgDels, Guid userId)
        {
            try
            {
                UserManager um = new UserManager(ApplicationName);

                return um.RemoveOrgsFromUser(idOrgDels, userId);
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Helper.RemoveOrgsFromUser code {0}", ex);

                return -1;
            }
        }

        /// <summary>
        /// Author: Hoang Van Tai
        /// add user to project 
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="userId"></param>
        /// <param name="userInviteID"></param>
        /// <param name="idPrjRoles"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public int AddUserToProjectsWithInvite(string ApplicationName, Guid userId, string userInviteID, string[] idPrjRoles, ref string message)
        {
            try
            {
                UserManager um = new UserManager(ApplicationName);

                return um.AddUserToProjects(userId, userInviteID,  idPrjRoles, ref message);
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Helper.AddUserToProjects code {0}", ex);

                return -1;
            }
        }

        /// <summary>
        /// Author: Hoang Van Tai
        /// Get list project from User by SA
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="userId"></param>
        /// <param name="msgError"></param>
        /// <returns></returns>
        public List<UserInProject> GetProjectListFromUserSA(string ApplicationName, string userId, ref string msgError)
        {
            try
            {
                UserManager pm = new UserManager(ApplicationName);

                return pm.GetProjectListFromUserSA(userId, ref msgError);
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Helper.GetProjectTypeListBySA code {0}", ex);

                return null;
            }
        }

        #endregion

        #region "RequestOrganizationBeOwner - SA"
        /// <summary>
        /// Author: Hoang Van Tai
        /// Set Org be owner user
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="userid"></param>
        /// <param name="orgId"></param>
        /// <param name="msgError"></param>
        /// <returns></returns>
        public int SetOrganBeOwnerUser(string ApplicationName, Guid userid, Guid orgId, ref string msgError)
        {
            try
            {
                RequestOrganizationBeOwnerManager rqm = new RequestOrganizationBeOwnerManager(ApplicationName);

                return rqm.SetOrganBeOwnerUser(userid, orgId, ref msgError);
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Helper.SetOrganBeOwnerUser code {0}", ex);

                return -1;
            }
        }

        /// <summary>
        /// Author: Hoang Van Tai
        /// Set Organization be owner user and assign user to Org
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="userid"></param>
        /// <param name="orgId"></param>
        /// <param name="msgError"></param>
        /// <returns></returns>
        public int SetOrganBeOwnerUserAndAssignUserToOrg(string ApplicationName, Guid userid, Guid orgId, ref string msgError)
        {
            try
            {
                RequestOrganizationBeOwnerManager rqm = new RequestOrganizationBeOwnerManager(ApplicationName);

                return rqm.SetOrganBeOwnerUserAndAssignUserToOrg(userid, orgId, ref msgError);
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Helper.SetOrganBeOwnerUserAndAssignUserToOrg code {0}", ex);

                return -1;
            }
        }

        /// <summary>
        /// Author: Hoang Van Tai
        /// Remove request be owner
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="Id"></param>
        /// <param name="msgError"></param>
        /// <returns></returns>
        public int RemoveRequestBeOwner(string ApplicationName, int Id, ref string msgError)
        {
            try
            {
                RequestOrganizationBeOwnerManager rqm = new RequestOrganizationBeOwnerManager(ApplicationName);

                return rqm.RemoveRequestBeOwner(Id, ref msgError);
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Helper.RemoveRequestBeOwner code {0}", ex);

                return -1;
            }
        }
        #endregion

        #region "ProjectType"
        /// <summary>
        /// Author: Hoang Van Tai
        /// Get list project type by SA
        /// /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="IsPM"></param>
        /// <param name="msgError"></param>
        /// <returns></returns>
        public List<ProjectType> GetProjectTypeListBySA(string ApplicationName, bool IsPM, ref string msgError)
        {
            try
            {
                ProjectTypeManager pm = new ProjectTypeManager(ApplicationName);

                return pm.GetProjectTypeListBySA(IsPM, ref msgError);
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Helper.GetProjectTypeListBySA code {0}", ex);

                return null ;
            }
        }

        /// <summary>
        /// Author: Hoang Van Tai
        /// Get list user for project
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="projetcId"></param>
        /// <returns></returns>
        public List<User> GetUsersForProject(string ApplicationName, int projetcId)
        {
            try
            {
                UserManager um = new UserManager(ApplicationName );

                return um.GetUsersForProject(projetcId);
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Helper.GetUsersForProject code {0}", ex);

                return null;
            }

        }
        #endregion

        #region "add user in application"
        /// <summary>
        /// Author: Hoang Van Tai
        /// Add user to application
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <param name="userName"></param>
        /// <returns></returns>
        public bool AddUserToApplication(string ApplicationName, string userName)
        {
            try
            {
                UserManager um = new UserManager(ApplicationName);

                return um.AddUserToApplication(userName);
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Helper.AddUserToApplication {0}", ex);

                return false;
            }
        }

        /// <summary>
        /// Author: Hoang Van Tai
        /// </summary>
        /// <param name="ApplicationName"></param>
        /// <returns></returns>
        public bool SynServiceUserToApplication(string ApplicationName)
        {
            try
            {
                UserManager um = new UserManager(ApplicationName);

                return um.SynServiceUserToApplication();
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("[" + ApplicationName + "] Helper.CreatServiceUserToApplication {0}", ex);

                return false;
            }
        }
        #endregion
    }
}